package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.pojo.Clazz;
import com.itheima.domain.pojo.PageResult;
import com.itheima.domain.query.ClazzQueryParam;
import com.itheima.mapper.ClazzMapper;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Sch
 * @Date: 2025/07/10 18:20
 * @Description:
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    // 添加
    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }

    // 根据id查询
    @Override
    public Clazz getById(Integer id){
        return clazzMapper.getById(id);
    }

    // 查询所有
    @Override
    public List<Clazz> findAll(){
        return clazzMapper.findAll();
    }

    // 分页查询
    @Override
    public PageResult page(ClazzQueryParam clazzQueryParam){
        //1.设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
        //2.执行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);
        //3.封装分页结果
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public boolean hasStudent(Integer clazzId) {
        return clazzMapper.findStudentCountByClazzId(clazzId) > 0;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteById(Integer id){
        clazzMapper.deleteById(id);
    }

    /**
     * 修改班级信息
     * @param clazz
     */
    @Override
    public void update(Clazz clazz){
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }
}
